home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp95 / freyja_t.z / freyja_t / file.c < prev    next >
C/C++ Source or Header  |  1992-04-14  |  10KB  |  512 lines

  1. /* FILE.C -- File-, Buffer-, and System-Oriented Commands
  2.  
  3.     Written July 1991 by Craig A. Finseth
  4.     Copyright 1991 by Craig A. Finseth
  5. */
  6.  
  7. #include "freyja.h"
  8.  
  9. #if defined(MSDOS)
  10. #include <time.h>
  11. #endif
  12.  
  13. char *getenv();
  14.  
  15. void F_FPathOpen_DirSep();    /* char *name */
  16.  
  17. /* ------------------------------------------------------------ */
  18.  
  19. /* Move to the begining of the buffer. */
  20.  
  21. void
  22. FBufBeg()
  23.     {
  24.     BMarkToPoint(mark);
  25.     BMoveToStart();
  26.     while (--uarg > 0) SearchNLF();
  27.     }
  28.  
  29.  
  30. /* ------------------------------------------------------------ */
  31.  
  32. /* Delete the current buffer. */
  33.  
  34. void
  35. FBufDel()
  36.     {
  37.     struct buffer *savebuf = cbuf;
  38.     struct window *wptr;
  39.  
  40.     uarg = 0;
  41.     if (!IS_SYS(cbuf->fname) && BIsMod(cbuf) &&
  42.          KAsk("Delete modified buffer?") != 'Y')
  43.         return;
  44.  
  45.     FBufPrev();
  46.     if (num_windows > 1) {
  47.         wptr = (cwin == &windows[0]) ? &windows[1] : &windows[0];
  48.         if (savebuf == BMarkBuf(wptr->sstart)) DWindOne();
  49.         }
  50.     BBufDelete(savebuf);
  51.     DScreenRange();
  52.     }
  53.  
  54.  
  55. /* ------------------------------------------------------------ */
  56.  
  57. /* Move to the end of the buffer. */
  58.  
  59. void
  60. FBufEnd()
  61.     {
  62.     if (isuarg)
  63.         FBufBeg();
  64.     else    {
  65.         BMarkToPoint(mark);
  66.         BMoveToEnd();
  67.         }
  68.     }
  69.  
  70.  
  71. /* ------------------------------------------------------------ */
  72.  
  73. /* Display a buffer list. */
  74.  
  75. void
  76. FBufList()
  77.     {
  78.     char buf[LINEBUFFSIZE];
  79.     struct buffer *bptr;
  80.  
  81.     if (!FMakeSys(SYS_BUFFLIST, TRUE)) return;
  82.  
  83.     for (bptr = cbuf + 1; bptr != cbuf; bptr++) {
  84.         if (bptr >= &buffers[NUMBUFFERS]) bptr = buffers;
  85.         if (!BIsFree(bptr) &&
  86.              (!c.g.skip_system || isuarg || !IS_SYS(bptr->fname))) {
  87.             xsprintf(buf, "%l    %s%s\n",
  88.                 BGetLength(bptr),
  89.                 BIsMod(bptr) ? "*" : " ",
  90.                 bptr->fname);
  91.             BInsStr(buf);
  92.             }
  93.         }
  94.     BMoveToStart();
  95.     DWindSwap();
  96.     }
  97.  
  98.  
  99. /* ------------------------------------------------------------ */
  100.  
  101. /* Switch to the next buffer in the ring. */
  102.  
  103. void
  104. FBufNext()
  105.     {
  106.     struct buffer *bptr;
  107.  
  108.     uarg = 0;
  109.     for (bptr = cbuf + 1; bptr != cbuf; bptr++) {
  110.         if (bptr >= &buffers[NUMBUFFERS]) bptr = buffers;
  111.         if (BIsFree(bptr)) continue;
  112.         if (c.g.skip_system && !isuarg && IS_SYS(bptr->fname)) continue;
  113.         break;
  114.         }
  115.     BBufGoto(bptr);
  116.     }
  117.  
  118.  
  119. /* ------------------------------------------------------------ */
  120.  
  121. /* Switch to the previous buffer in the ring. */
  122.  
  123. void
  124. FBufPrev()
  125.     {
  126.     struct buffer *bptr;
  127.  
  128.     uarg = 0;
  129.     for (bptr = cbuf; 1; ) {
  130.         if (bptr == buffers) bptr = &buffers[NUMBUFFERS];
  131.         bptr--;
  132.         if (bptr == cbuf) break;
  133.  
  134.         if (BIsFree(bptr)) continue;
  135.         if (c.g.skip_system && !isuarg && IS_SYS(bptr->fname)) continue;
  136.         break;
  137.         }
  138.     BBufGoto(bptr);
  139.     }
  140.  
  141.  
  142. /* ------------------------------------------------------------ */
  143.  
  144. /* Execute a shell command. */
  145.  
  146. #if !defined(NOEXEC)
  147. void
  148. FCommand()
  149.     {
  150.     char cmd_line[LINEBUFFSIZE];
  151.  
  152.     uarg = 0;
  153.     *cmd_line = NUL;
  154.     if (KGetStr("Command Line", cmd_line, LINEBUFFSIZE) != 'Y') return;
  155.  
  156.     TSetPoint(TMaxRow() - 1, 0);
  157.     TCLEOL();
  158.     TFini();
  159.  
  160.     system(cmd_line);
  161.  
  162.     xprintf("[Hit <CR> to return to Freyja]");
  163.  
  164.     while (getchar() != NL);
  165.  
  166.     TInit();
  167.     DNewDisplay();
  168.     }
  169. #endif
  170.  
  171.  
  172. /* ------------------------------------------------------------ */
  173.  
  174. /* Directory display. */
  175.  
  176. void
  177. FDIRED()
  178.     {
  179.     char fname[FNAMEMAX];
  180.     char buf[LINEBUFFSIZE];
  181. #if defined(UNIX)
  182.     char cmd_line[LINEBUFFSIZE];
  183.     FILE *fptr;
  184. #endif
  185. #if defined(MSDOS)
  186.     struct DOSFCB {
  187.         char DOSreserved[21];
  188.         char DOSattr;
  189.         int DOStime;
  190.         int DOSdate;
  191.         long DOSsize;
  192.         char DOSname[13];
  193.         } fcb;
  194.     int hr;
  195.     int ispm;
  196. #endif
  197.  
  198.     *fname = NUL;
  199.     if (KGetStr("File/directory name", fname, sizeof(fname)) != 'Y') return;
  200.  
  201. #if defined(UNIX)
  202.     if (!FMakeSys(SYS_DIRED, TRUE)) return;
  203.  
  204.     xsprintf(cmd_line, "ls %s %s", isuarg ? "-lg" : "", fname);
  205.     if ((fptr = popen(cmd_line, "r")) == NULL) {
  206.         DError("Cannot execute list command");
  207.         return;
  208.         }
  209.  
  210.     while (fgets(buf, sizeof(buf), fptr) != NULL) {
  211.         BInsStr(buf);
  212.         }
  213.     pclose(fptr);
  214. #endif
  215. #if defined(MSDOS)
  216.     if (!FMakeSys(SYS_DIRED, TRUE)) return;
  217.  
  218.     if (*fname == NUL) xstrcpy(fname, "*.*");
  219.     PSystem(0x1A, &fcb);            /* set disk transfer address */
  220.  
  221.         /* look for subdirectory + system + hidden files */
  222.     if (PSystem(0x4E, fname, 16 + 4 + 2) >= 0) {
  223.         do    {
  224.             BInsStr(fcb.DOSname);
  225.             if (!isuarg) {
  226.                 BInsChar(NL);
  227.                 continue;
  228.                 }
  229.             BInsStr("            " + min(BGetCol(), 12));
  230.             if (fcb.DOSattr & 16) {
  231.                 BInsStr("\t<DIR>");
  232.                 }
  233.             else    {
  234.                 xsprintf(buf, "\t%l", fcb.DOSsize);
  235.                 BInsStr(buf);
  236.                 }
  237.  
  238.             hr = (fcb.DOStime >> 11) & 0x1F;
  239.             ispm = 0;
  240.             if (hr > 11) {
  241.                 ispm = 1;
  242.                 hr -= 12;
  243.                 }
  244.             if (hr == 0) hr = 12;
  245.             xsprintf(buf, "\t%2d-%02d-%02d  %2d:%02d%c %c%c%c%c\n",
  246.                  (fcb.DOSdate >> 5) &  0xF,
  247.                   fcb.DOSdate       & 0x1F,
  248.                 ((fcb.DOSdate >> 9) & 0x7F) + 1980,
  249.                 hr,
  250.                 (fcb.DOStime >>  5) & 0x3F,
  251.                 ispm ? 'p' : 'a',
  252.                 fcb.DOSattr & 32 ? 'a' : SP,
  253.                 fcb.DOSattr &  4 ? 's' : SP,
  254.                 fcb.DOSattr &  2 ? 'h' : SP,
  255.                 fcb.DOSattr &  1 ? 'r' : SP);
  256.             BInsStr(buf);
  257.             } while (PSystem(0x4F) >= 0);
  258.         }
  259. #endif
  260.     BMoveToStart();
  261.     DWindSwap();
  262.     uarg = 0;
  263.     }
  264.  
  265.  
  266. /* ------------------------------------------------------------ */
  267.  
  268. /* Find file. */
  269.  
  270. void
  271. FFileFind()
  272.     {
  273.     struct buffer *bptr;
  274.  
  275.     uarg = 0;
  276. #if defined(SYSMGR)
  277.     if (!JGetFile("File to Find", filearg, TRUE)) return;
  278. #else
  279.     do    {
  280.         if (KGetStr("File to Find", filearg, sizeof(filearg)) != 'Y')
  281.             return;
  282.         } while (*filearg == NUL);
  283. #endif
  284.  
  285.     bptr = BBufFind(filearg);
  286.  
  287.     if (bptr == NULL) {
  288.         if (BBufCreate(filearg) == NULL) return;
  289.         if (!BFileRead()) DError("Read Error");
  290.         }
  291.     else    BBufGoto(bptr);
  292.     }
  293.  
  294.  
  295. /* ------------------------------------------------------------ */
  296.  
  297. /* Re-read a file into the current buffer. */
  298.  
  299. void
  300. FFileRead()
  301.     {
  302.     uarg = 0;
  303.     xstrcpy(filearg, cbuf->fname);
  304. #if defined(SYSMGR)
  305.     if (!JGetFile("File to Read", filearg, FALSE)) return;
  306. #else
  307.     do    {
  308.         if (KGetStr("File to Read", filearg, sizeof(filearg)) != 'Y')
  309.             return;
  310.         } while (*filearg == NUL);
  311. #endif
  312.  
  313.     if (BIsMod(cbuf) && KAsk("Throw away changes? ") != 'Y') {
  314.         DModeLine();
  315.         return;
  316.         }
  317.  
  318.     xstrcpy(cbuf->fname, filearg);
  319.     if (!BFileRead()) DError("Read error");
  320.     }
  321.  
  322.  
  323. /* ------------------------------------------------------------ */
  324.  
  325. /* Write the buffer to its file. */
  326.  
  327. void
  328. FFileSave()
  329.     {
  330.     uarg = 0;
  331.     if (!c.g.saving && !BIsMod(cbuf)) return;
  332.     DEcho("Writing File...");
  333.     if (!BFileWrite()) DError("Unable to save file");
  334.     DModeLine();
  335.     }
  336.  
  337.  
  338. /* ------------------------------------------------------------ */
  339.  
  340. /* Write the buffer to a different file. */
  341.  
  342. void
  343. FFileWrite()
  344.     {
  345.     uarg = 0;
  346.     xstrcpy(filearg, cbuf->fname);
  347. #if defined(SYSMGR)
  348.     if (!JGetFile("File to Write", filearg, FALSE)) return;
  349. #else
  350.     do    {
  351.         if (KGetStr("File to Write", filearg, sizeof(filearg)) != 'Y')
  352.             return;
  353.         } while (*filearg == NUL);
  354. #endif
  355.     xstrcpy(cbuf->fname, filearg);
  356.     DEcho("Writing File...");
  357.     if (!BFileWrite()) DError("Unable to save file");
  358.     DModeLine();
  359.     }
  360.  
  361.  
  362. /* ------------------------------------------------------------ */
  363.  
  364. /* Create and set up the system buffer. Return True on success or
  365. False on failure.  Zap the buffer if ERASE is true.  Otherwise, just
  366. move to the end. */
  367.  
  368. FLAG
  369. FMakeSys(name, erase)
  370.     char *name;
  371.     FLAG erase;
  372.     {
  373.     struct buffer *savebuf = cbuf;
  374.     struct buffer *bptr;
  375.  
  376.     uarg = 0;
  377.  
  378. /* find or make it */
  379.  
  380.     if ((bptr = BBufFind(name)) != NULL)
  381.         BBufGoto(bptr);
  382.     else if ((bptr = BBufCreate(name)) == NULL)
  383.         return(FALSE);
  384.  
  385. /* see if it is visible already */
  386.  
  387.     if (BMarkBuf(cwin->sstart) != cbuf) {
  388.         DWindTwo();
  389.         BBufGoto(savebuf);
  390.         DIncrDisplay();
  391.         DWindSwap();
  392.         BBufGoto(bptr);
  393.         DIncrDisplay();
  394.         }
  395.  
  396. /* clean buffer */
  397.  
  398.     if (erase) {
  399.         BMoveToStart();
  400.         BMarkToPoint(mark);
  401.         BMoveToEnd();
  402.         BRegDelete(mark);
  403.         }
  404.     else    BMoveToEnd();
  405.     return(TRUE);
  406.     }
  407.  
  408.  
  409. /* ------------------------------------------------------------ */
  410.  
  411. /* Open a file for input, searching the path.  NAME should not have a
  412. directory part.  If ACTUALNAME is not NULL, the actual name as opened
  413. will be filled in.  PExpand should be called if you want to remove all
  414. ambuguity.  It searches the following order:
  415.  
  416.     $FREYJADIR, $HOME, $PATH, current dir
  417. */
  418.  
  419. #if defined(MSDOS)
  420. #define SEPCHAR        ';'
  421. #define SEPSTR        ";"
  422. #endif
  423. #if defined(UNIX)
  424. #define SEPCHAR        ':'
  425. #define SEPSTR        ":"
  426. #endif
  427.  
  428. int
  429. FPathOpen(name, actualname)
  430.     char *name;
  431.     char *actualname;
  432.     {
  433.     int fd;
  434.     char tname[FNAMEMAX];
  435.     char tpath[2 * FNAMEMAX];
  436.     char *cptr;
  437.  
  438. #if defined(SYSMGR)
  439.     JGetDir(tpath);
  440.     tpath[strlen(tpath) - 1] = SEPCHAR;
  441. #else
  442. #if defined(MSDOS)
  443.     if (c.g.special == 'J') {
  444.         JGetDir(tpath);
  445.         tpath[strlen(tpath) - 1] = SEPCHAR;
  446.         }
  447.     else    *tpath = NUL;
  448. #else
  449.     *tpath = NUL;
  450. #endif
  451.     if ((cptr = getenv("FREYJADIR")) != NULL) {
  452.         strcat(tpath, cptr);
  453.         strcat(tpath, SEPSTR);
  454.         }
  455.  
  456.     if ((cptr = getenv("HOME")) != NULL) {
  457.         strcat(tpath, cptr);
  458.         strcat(tpath, SEPSTR);
  459.         }
  460.  
  461.     if ((cptr = getenv("PATH")) != NULL) {
  462.         if (*cptr == SEPCHAR)  strcat(tpath, ".");
  463.         strcat(tpath, cptr);
  464.         }
  465. #endif
  466.  
  467.     for (cptr = tpath; *cptr != NUL; cptr++) {
  468.         if (*cptr == SEPCHAR) *cptr = NUL;
  469.         }
  470.     *(cptr + 1) = NUL;
  471.  
  472.     for (cptr = tpath; *cptr != NUL; ) {
  473.         xstrcpy(tname, cptr);
  474.         F_FPathOpen_DirSep(tname);
  475.         strcat(tname, name);
  476. #if defined(MSDOS)
  477. #define O_RDONLY    0    /* dummy */
  478. #endif
  479.         if ((fd = open(tname, O_RDONLY, 0)) >= 0) {
  480.             if (actualname != NULL) xstrcpy(actualname, tname);
  481.             return(fd);
  482.             }
  483.         while (*cptr++ != NUL) ;
  484.         }
  485.  
  486.     if ((fd = open(name, O_RDONLY, 0)) >= 0) {
  487.         if (actualname != NULL) xstrcpy(actualname, name);
  488.         return(fd);
  489.         }
  490.  
  491.     return(-1);
  492.     }
  493.  
  494.  
  495. /* ------------------------------------------------------------ */
  496.  
  497. void
  498. F_FPathOpen_DirSep(name)
  499.     char *name;
  500.     {
  501. #if defined(UNIX)
  502.     if (*(name + strlen(name) - 1) != '/') strcat(name, "/");
  503. #endif
  504. #if defined(MSDOS)
  505.     if (*(name + strlen(name) - 1) != '/' &&
  506.         *(name + strlen(name) - 1) != '\\') strcat(name, "/");
  507. #endif
  508.     }
  509.  
  510.  
  511. /* end of FILE.C -- File-, Buffer-, and System-Oriented Commands */
  512.